import unittest

"""
 @author adun
 @date 2020/10/27 22:59
 字典（Dictionary）
 字典是一个无序、可变和有索引的集合。在 Python 中，字典用花括号编写，拥有键和值。【类似于Java的map，键值对】
 【与javaScript对象类型使用{}创建  json（键值对）格式】
"""


class MyTestCase(unittest.TestCase):
    def test_something(self):
        self.assertEqual(True, False)

    # 创建并打印字典：
    def test_create_dictionary(self):
        thisdict = {
            "brand": "Porsche",
            "model": "911",
            "year": 1963
        }
        print(thisdict)

    # 您可以通过在方括号内引用其键名来访问字典的项目：
    def test_get_val(self):
        thisdict = {
            "brand": "Porsche",
            "model": "911",
            "year": 1963
        }
        x = thisdict["model"]
        print(x)
        s=thisdict.get("model")
        print(s)

    # 通过引用其键名来更改特定项的值：
    def test_update_val(self):
        thisdict = {"brand": "Porsche", "model": "911", "year": 2019}
        print(thisdict)

    # 遍历字典
    # 您可以使用 for 循环遍历字典。
    #
    # 循环遍历字典时，返回值是字典的键，但也有返回值的方法。
    # 逐个打印字典中的所有键名：
    def test_for_val(self):
        thisdict = {
            "brand": "Porsche",
            "model": "911",
            "year": 1963
        }
        # 遍历键
        for x in thisdict:
            print(x)
        print("====================")
        # 遍历值
        for x in thisdict:
            print(thisdict[x])
        print("=============valus()获取值：")
        for x in thisdict.values():
            print(x)
        print("=============items()获取键与值：")
        for x, y in thisdict.items():
            print(x, y)
            print("{}={}".format(x,y))

    # 检查键是否存在
    # 要确定字典中是否存在指定的键，请使用 in 关键字：
    #
    # 实例
    # 检查字典中是否存在"model"：
    def test_have_val(self):
        thisdict = {
            "brand": "Porsche",
            "model": "911",
            "year": 1963
        }
        if "model" in thisdict:
            print("Yes, 'model' is one of the keys in the thisdict dictionary")

    # 添加项目
    # 通过使用新的索引键并为其赋值，可以将项目添加到字典中：
    def test_add_val(self):
        thisdict = {
            "brand": "Porsche",
            "model": "911",
            "year": 1963
        }
        thisdict["color"] = "red"
        print(thisdict)

    # 嵌套字典
    # 词典也可以包含许多词典，这被称为嵌套词典。
    def test_print_val(self):
        myfamily = {
            "child1": {
                "name": "Phoebe Adele",
                "year": 2002
            },
            "child2": {
                "name": "Jennifer Katharine",
                "year": 1996
            },
            "child3": {
                "name": "Rory John",
                "year": 1999
            }
        }

        print(myfamily)

    def test_print_dictionary(self):
        child1 = {
            "name": "Phoebe Adele",
            "year": 2002
        }
        child2 = {
            "name": "Jennifer Katharine",
            "year": 1996
        }
        child3 = {
            "name": "Rory John",
            "year": 1999
        }

        myfamily = {
            "child1": child1,
            "child2": child2,
            "child3": child3
        }
        print(myfamily)

    def test_constructor(self):
        thisdict = dict(brand="Porsche", model="911", year=1963)
        # 请注意，关键字不是字符串字面量
        # 请注意，使用了等号而不是冒号来赋值
        print(thisdict)


if __name__ == '__main__':
    unittest.main()
